home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.c
- Subject: Re: 16-bit memset?
- Date: 20 Mar 1996 13:02:59 -0600
- Organization: Usenet Fact Police (Undercover)
- Message-ID: <4ipkp3$1sc@solutions.solon.com>
- References: <joules-1803962243060001@badboy.mit.edu> <4ill6r$qnm@nntp.interaccess.com> <DoJABC.Hy7@iquest.net>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <DoJABC.Hy7@iquest.net>, Doug Miller <dlmiller@iquest.net> wrote:
- >+Julian Orbanes wrote:
- >+>I am looking quick way to set a series of 16-bit values to one value.
-
- >+>Analagous to how memset set works with 8-bit values. (For graphics
- >+>purposes).
-
- >char *bigstuff;
-
- >/* assuming that: */
- >/* 1) you have allocated storage for bigstuff */
- >/* 2) it is a null-terminated string of the desired FINAL length */
- >/* 3) you have loaded the value you wish to propagate into the first 2 bytes */
-
- >strncpy (bigstuff + 2, bigstuff, (strlen (bigstuff) - 2));
-
- Fascinating, but utterly wrong; it is illegal to depend on the semantics of
- copying a string onto itself, and the obvious implementation of strncpy will
- do precisely what you don't want here.
-
- Could you kindly shut up until you know C?
-
- (For the naive reader: The intent is that strncpy loops from the beginning,
- copying bytes, until it runs out, and that it will keep copying [pos] to
- [pos+2], so it will do "the right thing". The obvious failure is that if
- either byte of the 16 bit value is 0, you lose. The more subtle one is that
- strncpy can be implemented quite well in several other ways that break
- horribly on this.)
-
- This is probably the worst way to implement the operation, and would in most
- cases be dramatically slower than looping on 16-bit values and assigning, even
- on systems where it works, because it'll waste a lot of time checking every
- byte to see if it's a 0 - strncpy *does* have to check for the 0 byte, because
- it 0 fills past the end of the original string.
-
- -s
- --
- Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
- C/Unix wizard -- C/Unix questions? Send mail for help. No, really!
- FUCK the communications decency act. Goddamned government. [literally.]
- The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
-